<template>
  <div>
    <div class="app-container" v-if="opendetail == false">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
  <!--      <el-form-item label="ID" prop="activityId">-->
  <!--        <el-input-->
  <!--          v-model="queryParams.activityId"-->
  <!--          placeholder="请输入ID"-->
  <!--          clearable-->
  <!--          @keyup.enter="handleQuery"-->
  <!--        />-->
  <!--      </el-form-item>-->
  <!--      <el-form-item label="编号" prop="activityCode">-->
  <!--        <el-input-->
  <!--          v-model="queryParams.activityCode"-->
  <!--          placeholder="请输入编号"-->
  <!--          clearable-->
  <!--          @keyup.enter="handleQuery"-->
  <!--        />-->
  <!--      </el-form-item>-->
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
  <!--      <el-form-item label="发布人" prop="publisherName">-->
  <!--        <el-input-->
  <!--          v-model="queryParams.publisherName"-->
  <!--          placeholder="请输入发布人"-->
  <!--          clearable-->
  <!--          @keyup.enter="handleQuery"-->
  <!--        />-->
  <!--      </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 110px">
            <el-option
              v-for="dict in sys_actives_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否上架" prop="isOnline">
          <el-select v-model="queryParams.isOnline" placeholder="请选择状态" clearable style="width: 110px">
            <el-option
                v-for="dict in sys_actives_shelves"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" style="width: 308px">
          <el-date-picker
            v-model="daterangeReleaseTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="审核时间" style="width: 308px">
          <el-date-picker
            v-model="daterangeAuditTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
  <!--      <el-form-item label="图片id" prop="imageId">-->
  <!--        <el-input-->
  <!--          v-model="queryParams.imageId"-->
  <!--          placeholder="请输入图片id"-->
  <!--          clearable-->
  <!--          @keyup.enter="handleQuery"-->
  <!--        />-->
  <!--      </el-form-item>-->
  <!--      <el-form-item label="商家ID" prop="merchantId">-->
  <!--        <el-input-->
  <!--          v-model="queryParams.merchantId"-->
  <!--          placeholder="请输入商家ID"-->
  <!--          clearable-->
  <!--          @keyup.enter="handleQuery"-->
  <!--        />-->
  <!--      </el-form-item>-->

      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['masterrabbit-kaixuan:activities:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['masterrabbit-kaixuan:activities:export']"
          >导出</el-button>
        </el-col>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="activitiesList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
  <!--      <el-table-column label="ID" align="center" prop="activityId" />-->
        <el-table-column label="编号" align="center" prop="activityCode" />
        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="发布人" align="center" prop="publisherName" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_actives_state" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="是否上架" align="center" prop="isOnline">
          <template #default="scope">
            <dict-tag :options="sys_actives_shelves" :value="scope.row.isOnline"/>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.releaseTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发布内容" align="center" prop="content" />
  <!--      <el-table-column label="图片id" align="center" prop="imageId" />-->
  <!--      <el-table-column label="商家ID" align="center" prop="merchantId" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-if="scope.row.isOnline == 1" :disabled="scope.row.status == 3 || scope.row.status == 2" type="primary" @click="uphandleUpdate(scope.row)" v-hasPermi="['masterrabbit-kaixuan:activities:edit']">上架</el-button>
            <el-button v-if="scope.row.isOnline == 2" type="primary" style="background-color: #ed5565" @click="downhandleUpdate(scope.row)" v-hasPermi="['masterrabbit-kaixuan:activities:edit']">下架</el-button>
            <el-button type="primary" @click="getdetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改活动对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="activitiesRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="编号" prop="activityCode">
            <el-input v-model="form.activityCode" placeholder="请输入编号" />
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>
          <el-form-item label="发布人" prop="publisherName">
            <el-input v-model="form.publisherName" placeholder="请输入发布人" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in sys_actives_state"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否上架" prop="isOnline">
            <el-input v-model="form.isOnline" placeholder="请输入是否上架" />
          </el-form-item>
          <el-form-item label="发布时间" prop="releaseTime">
            <el-date-picker clearable
              v-model="form.releaseTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择发布时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="审核时间" prop="auditTime">
            <el-date-picker clearable
              v-model="form.auditTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择审核时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="发布内容">
            <editor v-model="form.content" :min-height="192"/>
          </el-form-item>
  <!--        <el-form-item label="图片id" prop="imageId">-->
  <!--          <el-input v-model="form.imageId" placeholder="请输入图片id" />-->
  <!--        </el-form-item>-->
  <!--        <el-form-item label="商家ID" prop="merchantId">-->
  <!--          <el-input v-model="form.merchantId" placeholder="请输入商家ID" />-->
  <!--        </el-form-item>-->
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>

    <div v-if="opendetail == true" class="activity-detail-container">
      <el-button type="primary" @click="backlast">返回</el-button>
      <el-form :model="details" label-width="100px" class="custom-activity-form">
        <el-form-item label="标题">
          <el-tag :closable="false">{{ details.title }}</el-tag>
        </el-form-item>

        <el-form-item label="发布人">
          <el-tag :closable="false">{{ details.publisherName }}</el-tag>
        </el-form-item>

        <el-form-item label="发布时间">
          <el-tag :closable="false">{{ details.releaseTime }}</el-tag>
        </el-form-item>

        <el-form-item label="内容">
          <el-textarea v-model="details.content" rows="4" :disabled="true" />
        </el-form-item>

        <el-form-item label="图片">
          <el-image :src="imageUrl" alt="活动图片" fit="cover" width="100" height="100" />
        </el-form-item>
      </el-form>
      <el-form>
        <el-button  type="primary" @click="crosshandleUpdate(details)" v-hasPermi="['masterrabbit-kaixuan:activities:edit']">审核通过</el-button>
        <el-button style="background-color: #ed5565" type="primary" @click="uncrosshandleUpdate(details)" v-hasPermi="['masterrabbit-kaixuan:activities:edit']">审核未通过</el-button>
      </el-form>
    </div>

  </div>
</template>

<script setup name="Activities">
import { listActivities, getActivities, delActivities, addActivities, updateActivities } from "@/api/masterrabbit-kaixuan/activities";

const { proxy } = getCurrentInstance();
const { sys_actives_state } = proxy.useDict('sys_actives_state');
const { sys_actives_shelves } = proxy.useDict('sys_actives_shelves');

const activitiesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeReleaseTime = ref([]);
const daterangeAuditTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activityId: null,
    activityCode: null,
    title: null,
    publisherName: null,
    status: null,
    isOnline: null,
    releaseTime: null,
    auditTime: null,
    content: null,
    imageId: null,
    merchantId: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询活动列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeReleaseTime && '' != daterangeReleaseTime) {
    queryParams.value.params["beginReleaseTime"] = daterangeReleaseTime.value[0];
    queryParams.value.params["endReleaseTime"] = daterangeReleaseTime.value[1];
  }
  if (null != daterangeAuditTime && '' != daterangeAuditTime) {
    queryParams.value.params["beginAuditTime"] = daterangeAuditTime.value[0];
    queryParams.value.params["endAuditTime"] = daterangeAuditTime.value[1];
  }
  listActivities(queryParams.value).then(response => {
    activitiesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    activityId: null,
    activityCode: null,
    title: null,
    publisherName: null,
    status: null,
    isOnline: null,
    releaseTime: null,
    auditTime: null,
    content: null,
    imageId: null,
    merchantId: null
  };
  proxy.resetForm("activitiesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeReleaseTime.value = [];
  daterangeAuditTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.activityId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加活动";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _activityId = row.activityId || ids.value
  getActivities(_activityId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改活动";
  });
}
/** 上架按钮操作 */
function uphandleUpdate(row) {
  reset();
  const _activityId = row.activityId || ids.value
  getActivities(_activityId).then(response => {
    form.value = response.data;
    console.log(form)
    form.value.isOnline = 2;
    // open.value = true;
    title.value = "修改活动";
    updateActivities(form.value).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  });
}

/** 下架按钮操作 */
function downhandleUpdate(row) {
  reset();
  const _activityId = row.activityId || ids.value
  getActivities(_activityId).then(response => {
    form.value = response.data;
    form.value.isOnline = 1;
    // open.value = true;
    title.value = "修改活动";
    updateActivities(form.value).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  });
}

/** 通过审核按钮操作 */
function crosshandleUpdate(row){
  reset();
  const _activityId = row.activityId || ids.value
  getActivities(_activityId).then(response => {
    form.value = response.data;
    form.value.isOnline = 1;
    form.value.status = 1;
    form.value.auditTime = gettime();
    // open.value = true;
    title.value = "修改活动";
    console.log(form.value)
    updateActivities(form.value).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  });
  backlast();
}
/** 未通过审核按钮操作 */
function uncrosshandleUpdate(row){
  reset();
  const _activityId = row.activityId || ids.value
  getActivities(_activityId).then(response => {
    form.value = response.data;
    form.value.auditTime = gettime();
    form.value.status = 3;
    // open.value = true;
    title.value = "修改活动";
    updateActivities(form.value).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  });
  backlast();
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activitiesRef"].validate(valid => {
    if (valid) {
      if (form.value.activityId != null) {
        updateActivities(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addActivities(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _activityIds = row.activityId || ids.value;
  proxy.$modal.confirm('是否确认删除活动编号为"' + _activityIds + '"的数据项？').then(function() {
    return delActivities(_activityIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('masterrabbit-kaixuan/activities/export', {
    ...queryParams.value
  }, `activities_${new Date().getTime()}.xlsx`)
}

/** 跳转详情页按钮操作 */
const opendetail = ref(false)
const details = ref({})

function getdetail(row){
  opendetail.value = true;
  console.log(row)
  details.value = row
}

function backlast(){
  opendetail.value = false;
}


/** 获取当前时间 年月日 的方法 */
function gettime() {
  const currentDate = new Date();

// 获取年月日
  const year = currentDate.getFullYear();
  const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
  const day = currentDate.getDate().toString().padStart(2, '0');

// 获取时分秒
  const hours = currentDate.getHours().toString().padStart(2, '0');
  const minutes = currentDate.getMinutes().toString().padStart(2, '0');
  const seconds = currentDate.getSeconds().toString().padStart(2, '0');

// 格式化为 YYYY-MM-DD HH:mm:ss
  const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  console.log(formattedDateTime); // 输出格式化后的当前日期
  return formattedDateTime
}

getList();
</script>
<style scoped>
.activity-detail-container {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  flex-direction: column; /* 内容纵向排列 */
  min-height: 100vh; /* 确保容器至少占满整个视口高度 */
  padding: 20px;
}

.custom-activity-form {
  width: 100%;
  max-width: 800px; /* 最大宽度 */
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px; /* 可选，增加圆角 */
}

.el-form-item {
  display: flex;
  align-items: center; /* 垂直居中 */
  justify-content: space-between; /* 均匀分布 label 和内容 */
  margin-bottom: 20px; /* 每个表单项之间的间距 */
}

.el-form-item label {
  flex: 0 0 120px; /* 固定标签宽度 */
  font-weight: bold; /* 标签字体加粗 */
}

.el-form-item .el-tag, .el-form-item .el-textarea, .el-form-item .el-image {
  flex-grow: 1; /* 使内容部分自动填充剩余空间 */
}

.el-button {
  margin-bottom: 20px; /* 按钮与表单的间距 */
}
</style>
